function ajoutLigneSaisie(type)
{
	if (type == 'section')
		nouvelle_section();
	else if (type == 'contact')
		nouveau_contact();
}

function validationFormulaire ()
{
	/* Get the static data here*/
	var ad_id = document.getElementById('ad_id').value;
	var ad_nom = document.getElementById('ad_nom').value;
	
	save_asso(ad_id, ad_nom);

	/* Get the address data via in a table form */
	var tab_addr = new Array(5);
	var addr_field = ['mt', 'ad_addr1', 'ad_addr2', 'ad_cp','ad_ville'];

	tab_addr[0] = document.getElementById('ad_id').value;
	for (i = 1; i < addr_field.length; i++)
		tab_addr[i] = document.getElementById(addr_field[i]).value;

	save_addr(tab_addr);

	/* Handle the section input here */
	var tableau_section = extrait_section_json();

	/* Save the sections, one by one */

	var section_tab = new Array(2);
	section_tab[0] = ad_id;

	var i = 0;
	while (tableau_section.length > i) {
		section_tab[1] = tableau_section[i++];
		save_section(section_tab);
	}

	/* Handle the contact input here */
	var tableau_contact = extrait_contact_json();

	/* Save the sections, one by one */

	/* Debug output - write data to the page rather than doing an ajax call for now */
	var elem = document.getElementById("debug_output");
	var elem2 = document.getElementById("debug_output").firstChild;
	if (elem2 != null)
		elem.removeChild(elem2);

	var newElem = document.createElement("span");
	newElem.innerHTML += "Nom asso: " + JSON.stringify(ad_nom) + "<BR/>";
	newElem.innerHTML += "Adresse: "  + JSON.stringify(tab_addr) + "<BR/>";
	newElem.innerHTML += "Sections: " + JSON.stringify(tableau_section) + "<BR/>";
	newElem.innerHTML += "Contacts: " + JSON.stringify(tableau_contact) + "<BR/>";
	elem.appendChild(newElem);
}

function init()
{
	document.getElementById('ad_id').value = "";
	document.getElementById('ad_nom').value = "";
	document.getElementById('ad_addr1').value = "";
	document.getElementById('ad_addr2').value = "";
	document.getElementById('ad_cp').value = "";
	document.getElementById('ad_ville').value = "";
	
	var elem = document.getElementById("debug_output");
	var elem2 = document.getElementById("debug_output").firstChild;
	if (elem2 != null)
		elem.removeChild(elem2);

}


function save_asso(asso_id, asso_nom)
{
	// Make asynch call
	var asynchHandler = new asynchHandlerClass();
	asynchHandler.init();
	asynchHandler.setEventSink(save_asso_CallBack);
	var url = 'ajax-save-json-array.aspx?type=asso&array=["'+ asso_id + '","' + asso_nom + '"]'
	asynchHandler.sendHttpRequest('GET', url );
}

function save_asso_CallBack()
{
	var result = asynchHandler.receivedData;
	if (result != "SUCCESS") {
		document.getElementById('asso_head').className = "error";
	} else {
		document.getElementById('asso_head').className = "success";
	}
}

function save_section(section)
{
	var asynchHandler = new asynchHandlerClass();
	asynchHandler.init();
	asynchHandler.setEventSink(save_section_CallBack);
	var url = 'ajax-save-json-array.aspx?type=section&array=' + JSON.stringify(section);
	alert(url);
	asynchHandler.sendHttpRequest('GET', url );
}

function save_section_CallBack()
{
	var result = asynchHandler.receivedData;
	if (result != "SUCCESS") {
		document.getElementById('section_head').className = "error";
	} else {
		document.getElementById('section_head').className = "success";
	}
}

function save_addr(tab_addr)
{
	// Make asynch call
	var asynchHandler = new asynchHandlerClass();
	asynchHandler.init();
	asynchHandler.setEventSink(save_addr_CallBack);
	var url = 'ajax-save-json-array.aspx?type=addr&array=' + JSON.stringify(tab_addr);
	asynchHandler.sendHttpRequest('GET', url );
}

function save_addr_CallBack()
{
	var result = asynchHandler.receivedData;
	if (result != "SUCCESS") {
		document.getElementById('addr_head').className = "error";
	} else {
		document.getElementById('addr_head').className = "success";
	}
}

